<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑,路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="medium">
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select placeholder="请选择频道" v-model="channelId">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              :label="channel.name"
              :value="channel.id"
              v-for="(channel,index) in channels"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            v-model="rangeDate"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadArticles(1)">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 卡片2 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">根据筛选条件查询到 {{ totalCount }} 条结果:</div>
      <!-- 数据列表 -->
      <el-table :data="articles" stripe style="width: 100%" class="list-table">
        <el-table-column prop="date" label="封面">
          <template>
            <el-image
              style="width: 66px; height: 66px"
              :src="require('./login_bg.png')"
              fit="cover"
            ></el-image>
            <!-- <img v-if="scope.row.cover.images[0]" class="articleCover" :src="scope.row.cover.images[0]" alt="">
            <img v-else class="articleCover" src="./login_bg.png" alt="">-->
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column label="状态">
          <!-- 如果要在自定义列模板中获取当前遍历项数据，则需要在template上声明 -->
          <template slot-scope="scope">
            <el-tag
              :type="articleStatus[scope.row.status].type"
            >{{articleStatus[scope.row.status].text}}</el-tag>
            <!-- <el-tag v-else-if="scope.row.status === 1" type="info">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger">审核失败</el-tag>
            <el-tag v-else-if="scope.row.status === 4" type="warning">已删除</el-tag>-->
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="medium" circle type="primary"
            @click="$router.push('/publish?id='+scope.row.id)"
             icon="el-icon-edit"></el-button>
            <el-button
              size="medium"
              circle
              type="danger"
              icon="el-icon-delete"
              @click="onDeleteArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页页码 -->

      <el-pagination
        :page-size="pageSize"
        @current-change="onCurrentChange"
        background
        layout="prev, pager, next"
        :total="totalCount"
        :current-page.sync="page"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, getArticlesChannels, deleteArticle } from "@/api/article";

export default {
  name: "ArticleIndex",
  components: {},
  props: {},
  data() {
    return {
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: ""
      },
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ],
      articles: [], //文章数据列表
      articleStatus: [
        {
          status: 0,
          text: "草稿",
          type: "info"
        },
        {
          status: 1,
          text: "待审核",
          type: ""
        },
        {
          status: 2,
          text: "审核通过",
          type: "success"
        },
        {
          status: 3,
          text: "审核失败",
          type: "danger"
        },
        {
          status: 4,
          text: "已删除",
          type: "warning"
        }
      ],
      totalCount: 0, // 总数据条数
      pageSize: 6,
      status: null, // 文章状态
      channels: [], //文章频道数据
      channelId: null, //文章频道id
      rangeDate: [], // 筛选日期范围
      page: 1 , // 当前页码
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadArticles();
    this.loadArticlesChannels();
  },
  mounted() {},
  methods: {
    loadArticles(page = 1) {
      getArticles({
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null, // 开始日期
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null // 结束日期
      }).then(res => {
        // console.log(res)
        const { results, total_count: totalCount } = res.data.data;
        this.articles = results;
        this.totalCount = totalCount;
      });
    },
    onCurrentChange(page) {
      this.loadArticles(page);
    },
    loadArticlesChannels() {
      getArticlesChannels().then(res => {
        this.channels = res.data.data.channels;
      });
    },
    onDeleteArticle(articleId) {
      this.$confirm("确认删除?", "删除警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 删除逻辑
        console.log('删除')
        deleteArticle(articleId).then(res =>{
            console.log(res)
            // 更新文章数据列表
            this.loadArticles(this.page)
        })
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.filter-card {
  margin-bottom: 20px;
}
.list-table {
  margin-bottom: 20px;
}
.articleCover {
  widows: 100px;
  background-size: cover; // 短边完全显示
}
</style>